//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "IncrementalStatistics.h"
using namespace Cephei::QL::Math::Statistics;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Statistics::CIncrementalStatistics::CIncrementalStatistics () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phIncrementalStatistics = NULL;
#endif
        _ppIncrementalStatistics = new boost::shared_ptr<QuantLib::IncrementalStatistics> (new QuantLib::IncrementalStatistics ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Statistics::CIncrementalStatistics::CIncrementalStatistics (boost::shared_ptr<QuantLib::IncrementalStatistics>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIncrementalStatistics = NULL;
#endif
	_ppIncrementalStatistics = &childNative;
    
}
Cephei::QL::Math::Statistics::CIncrementalStatistics::CIncrementalStatistics (QuantLib::IncrementalStatistics& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phIncrementalStatistics = NULL;
#endif
	_ppIncrementalStatistics = new boost::shared_ptr<QuantLib::IncrementalStatistics> (&childNative);
    
    _IncrementalStatisticsOwner = owner;
    
}

Cephei::QL::Math::Statistics::CIncrementalStatistics::CIncrementalStatistics (CIncrementalStatistics^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIncrementalStatistics = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIncrementalStatistics = new boost::shared_ptr<QuantLib::IncrementalStatistics> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Statistics::CIncrementalStatistics::CIncrementalStatistics (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIncrementalStatistics = NULL;
#endif
	if (!t->IsSubclassOf(CIncrementalStatistics::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Statistics::CIncrementalStatistics::CIncrementalStatistics (QuantLib::Handle<QuantLib::IncrementalStatistics>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIncrementalStatistics = &childNative;
	_ppIncrementalStatistics = &static_cast<boost::shared_ptr<QuantLib::IncrementalStatistics>>(childNative.currentLink());
    
    _IncrementalStatisticsOwner = owner;
}
Cephei::QL::Math::Statistics::CIncrementalStatistics::CIncrementalStatistics (QuantLib::Handle<QuantLib::IncrementalStatistics> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIncrementalStatistics = &childNative;
	_ppIncrementalStatistics = &static_cast<boost::shared_ptr<QuantLib::IncrementalStatistics>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Statistics::CIncrementalStatistics::CIncrementalStatistics (QuantLib::IncrementalStatistics childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIncrementalStatistics = NULL;
#endif
	_ppIncrementalStatistics = new boost::shared_ptr<QuantLib::IncrementalStatistics> (new QuantLib::IncrementalStatistics (childNative));
    
}
#endif

Cephei::QL::Math::Statistics::CIncrementalStatistics::~CIncrementalStatistics ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppIncrementalStatistics != NULL)
    {
	    delete _ppIncrementalStatistics;
        _ppIncrementalStatistics = NULL;
    }
}
Cephei::QL::Math::Statistics::CIncrementalStatistics::!CIncrementalStatistics ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppIncrementalStatistics != NULL)
    {
	    delete _ppIncrementalStatistics;
    }
}
QuantLib::IncrementalStatistics& Cephei::QL::Math::Statistics::CIncrementalStatistics::GetReference ()
{
    if (_ppIncrementalStatistics == NULL) throw gcnew NativeNullException ();
	return **_ppIncrementalStatistics;
}
boost::shared_ptr<QuantLib::IncrementalStatistics>& Cephei::QL::Math::Statistics::CIncrementalStatistics::GetShared ()
{
    if (_ppIncrementalStatistics == NULL) throw gcnew NativeNullException ();
	return *_ppIncrementalStatistics;
}
QuantLib::IncrementalStatistics* Cephei::QL::Math::Statistics::CIncrementalStatistics::GetPointer ()
{
    if (_ppIncrementalStatistics == NULL) throw gcnew NativeNullException ();
	return &**_ppIncrementalStatistics;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::IncrementalStatistics>& Cephei::QL::Math::Statistics::CIncrementalStatistics::GetHandle ()
{
	if (_phIncrementalStatistics == NULL)
	{
		_phIncrementalStatistics = new Handle<QuantLib::IncrementalStatistics> (*_ppIncrementalStatistics);
	}
	return *_phIncrementalStatistics;
}
#endif
bool Cephei::QL::Math::Statistics::CIncrementalStatistics::HasNative () 
{
	return (_ppIncrementalStatistics != NULL);
}

Cephei::QL::Math::Statistics::IIncrementalStatistics^ Cephei::QL::Math::Statistics::CIncrementalStatistics::Add (Double value, Microsoft::FSharp::Core::FSharpOption<Double>^ weight)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _value = (QuantLib::Real)ValueHelper::Convert (value);
        QuantLib::Real _weight = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (weight) ? (QuantLib::Real)ValueHelper::Convert (weight->Value) : 1.0); //9a
    	(*_ppIncrementalStatistics)->add ( _value,  _weight );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CIncrementalStatistics::DownsideDeviation::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIncrementalStatistics)->downsideDeviation ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CIncrementalStatistics::DownsideVariance::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIncrementalStatistics)->downsideVariance ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CIncrementalStatistics::ErrorEstimate::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIncrementalStatistics)->errorEstimate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CIncrementalStatistics::Kurtosis::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIncrementalStatistics)->kurtosis ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CIncrementalStatistics::Max::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIncrementalStatistics)->max ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CIncrementalStatistics::Mean::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIncrementalStatistics)->mean ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CIncrementalStatistics::Min::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIncrementalStatistics)->min ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Statistics::IIncrementalStatistics^ Cephei::QL::Math::Statistics::CIncrementalStatistics::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppIncrementalStatistics)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Statistics::CIncrementalStatistics::Samples::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppIncrementalStatistics)->samples ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CIncrementalStatistics::Skewness::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIncrementalStatistics)->skewness ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CIncrementalStatistics::StandardDeviation::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIncrementalStatistics)->standardDeviation ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CIncrementalStatistics::Variance::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIncrementalStatistics)->variance ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CIncrementalStatistics::WeightSum::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIncrementalStatistics)->weightSum ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Statistics::IIncrementalStatistics^ Cephei::QL::Math::Statistics::CIncrementalStatistics_Factory::Create ()
{
    return gcnew CIncrementalStatistics ();
}
